package com.androdro.bluenote.birdland1521;
/**
 * A Night at Birdland with Art Blakey Quintet
 * Mayreh.java
 *
 * @author Hiroaki Sakuma <sakuma@androdro.com>
 * @version $Id$
 *
 * Copyright (C) androdro lab. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of the nor the names of its contributors may be used to
 *   endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class Mayreh
	extends
		SurfaceView
	implements
		SurfaceHolder.Callback,
		OnTouchListener,
		SensorEventListener,
		Runnable,
		Birdland1521 {

	/**
   * This class is droid robot.
   *
   */

	private SplitKick root;

	private Resources r;
	private SurfaceHolder holder;
	private Thread t;
	private SensorManager mSensorManager;
	private Map<Integer, WeeDot> wds = new HashMap<Integer, WeeDot>();

	private float px = 0;
	private float py = 0;
	private Integer currentDroid = new Integer(0);
	private boolean touchDown, moving, mIsMagneticFieldSensor, mIsAccelerometerSensor;


	Mayreh(SplitKick root) {

		super(root);
		this.root = root;

		try {

			this.r = this.root.getResources();

			this.holder = getHolder();
			this.holder.addCallback(this);
			this.holder.setFixedSize(getWidth(), getHeight());
			this.mSensorManager = (SensorManager)this.root.getSystemService(this.root.SENSOR_SERVICE);


			setOnTouchListener(this);

			List<Sensor>sensors = this.mSensorManager.getSensorList(Sensor.TYPE_ALL);

			for (Sensor sensor:sensors) {
				if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
					this.mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
					mIsMagneticFieldSensor = true;
				}
				if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
					this.mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
					mIsAccelerometerSensor = true;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this.root, e.toString() + "on " + getClass().toString() + " " + e.getStackTrace()[0].getMethodName(), Toast.LENGTH_LONG).show();
		}

	}


	public void surfaceCreated(SurfaceHolder holder) {

		this.t = new Thread(this);
		this.t.start();

	}


	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
	}


	public void surfaceDestroyed(SurfaceHolder holder) {
		this.t = null;
	}


	public void run() {

		Canvas canvas;
		WeeDot wd;
		Paint p;
		long time;

		try {

			while(this.t != null) {

				time = System.currentTimeMillis();
//System.out.println(time);

				canvas = this.holder.lockCanvas();
				canvas.drawColor(Color.WHITE);
				wd = this.wds.get(currentDroid);
				p = new Paint();

				p.setAlpha(192);

				if (wd == null && touchDown && !moving) {

					wd = WeeDotFactory.getInstance(this.root).createDroid();

					switch((int)(py * 10) % 3) {
						case 1:
							wd.setScale(0.5);
							break;
						case 2:
							wd.setScale(0.75);
							break;
						default:
							wd.setScale(1.0);
					}

					this.wds.put(currentDroid, wd);

				}

				if (wd != null && this.px != 0 && this.py != 0) {

					wd.setXY(this.px, this.py);

					switch((int)(time / 100) % 12) {
						case 1:
						case 5:
							wd.setDegrees(5.0);
							break;
						case 2:
						case 4:
							wd.setDegrees(8.0);
							break;
						case 3:
							wd.setDegrees(10.0);
							break;
						case 7:
						case 11:
							wd.setDegrees(355.0);
							break;
						case 8:
						case 10:
							wd.setDegrees(352.0);
							break;
						case 9:
							wd.setDegrees(350.0);
							break;
						default:
							wd.setDegrees(0.0);
					}

					canvas.drawBitmap(wd.getBitmap(), wd.getCenterX() - 50, wd.getCenterY() + 50, p);

				}


				for (int i = 0; i < currentDroid.intValue(); i++) {

					WeeDot wddraw = this.wds.get(i);
					if(wddraw != null) {

						float alpha = wddraw.getAlpha();
						alpha = alpha / 1.05f;
						wddraw.setAlpha(alpha);

						if (alpha < 2.0f) {
							//wddraw = null;
							this.wds.remove(i);
						} else {
							canvas.drawBitmap(wddraw.getBitmap(), wddraw.getCenterX() - 50, wddraw.getCenterY() + 50, wddraw.getPaint());
						}

					}

				}

				if (wd != null && !touchDown && !moving) {
					wd.setScale(wd.getScale() * 0.8);
					currentDroid = new Integer(currentDroid.intValue() + 1);
				}

				this.holder.unlockCanvasAndPost(canvas);

				Thread.sleep(40);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	protected void onDraw(Canvas canvas) {

	}


	public boolean onTouch(View view, MotionEvent event) {

		int action = event.getAction();
		WeeDot wd = this.wds.get(currentDroid);

		this.px = event.getX();
		this.py = event.getY();

		//switch (action & MotionEvent.ACTION_MASK) {
		switch (action) {

			case MotionEvent.ACTION_DOWN:
				moving = false;
				touchDown = true;
				break;

			case MotionEvent.ACTION_MOVE:
				moving = true;
				break;

			case MotionEvent.ACTION_UP:
				moving = false;
				touchDown = false;
				break;

		}

//		Toast.makeText(this.c, "X: " + event.getX() + ", Y: " + event.getY(), Toast.LENGTH_SHORT).show();
		return true;

	}


	public void onSensorChanged(SensorEvent event) {

		float[] orientationValues = new float[3];
		float[] magneticValues = new float[3];

		if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
		} else {

			switch (event.sensor.getType()) {
				case Sensor.TYPE_MAGNETIC_FIELD:
				case Sensor.TYPE_ACCELEROMETER:
			}

		}

	}


	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}


}
